package de.fhk.fst.dvr.association

import de.fhk.fst.dvr.BaseController

class UserGroupAssociationController extends BaseController {

  static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

  def index = {
    redirect(action: "list", params: params)
  }

  def list = {
    params.max = Math.min(params.max ? params.int('max') : 10, 100)
    [userGroupAssociationInstanceList: UserGroupAssociation.list(params), userGroupAssociationInstanceTotal: UserGroupAssociation.count()]
  }

  def create = {
    def userGroupAssociationInstance = new UserGroupAssociation()
    userGroupAssociationInstance.properties = params
    return [userGroupAssociationInstance: userGroupAssociationInstance]
  }

  def save = {
    def userGroupAssociationInstance = new UserGroupAssociation(params)
    if (userGroupAssociationInstance.save(flush: true)) {
      flash.message = "${message(code: 'default.created.message', args: [message(code: 'userGroupAssociation.label', default: 'UserGroupAssociation'), userGroupAssociationInstance.id])}"
      redirect(action: "show", id: userGroupAssociationInstance.id)
    } else {
      render(view: "create", model: [userGroupAssociationInstance: userGroupAssociationInstance])
    }
  }

  def show = {
    def userGroupAssociationInstance = UserGroupAssociation.get(params.id)
    if (!userGroupAssociationInstance) {
      flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'userGroupAssociation.label', default: 'UserGroupAssociation'), params.id])}"
      redirect(action: "list")
    } else {
      [userGroupAssociationInstance: userGroupAssociationInstance]
    }
  }

  def edit = {
    def userGroupAssociationInstance = UserGroupAssociation.get(params.id)
    if (!userGroupAssociationInstance) {
      flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'userGroupAssociation.label', default: 'UserGroupAssociation'), params.id])}"
      redirect(action: "list")
    } else {
      return [userGroupAssociationInstance: userGroupAssociationInstance]
    }
  }

  def update = {
    def userGroupAssociationInstance = UserGroupAssociation.get(params.id)
    if (userGroupAssociationInstance) {
      if (params.version) {
        def version = params.version.toLong()
        if (userGroupAssociationInstance.version > version) {
          userGroupAssociationInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [
            message(code: 'userGroupAssociation.label', default: 'UserGroupAssociation')
          ]
          as Object[], "Another user has updated this UserGroupAssociation while you were editing")
          render(view: "edit", model: [userGroupAssociationInstance: userGroupAssociationInstance])
          return
        }
      }
      userGroupAssociationInstance.properties = params
      if (!userGroupAssociationInstance.hasErrors() && userGroupAssociationInstance.save(flush: true)) {
        flash.message = "${message(code: 'default.updated.message', args: [message(code: 'userGroupAssociation.label', default: 'UserGroupAssociation'), userGroupAssociationInstance.id])}"
        redirect(action: "show", id: userGroupAssociationInstance.id)
      } else {
        render(view: "edit", model: [userGroupAssociationInstance: userGroupAssociationInstance])
      }
    } else {
      flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'userGroupAssociation.label', default: 'UserGroupAssociation'), params.id])}"
      redirect(action: "list")
    }
  }

  def delete = {
    def userGroupAssociationInstance = UserGroupAssociation.get(params.id)
    if (userGroupAssociationInstance) {
      try {
        userGroupAssociationInstance.delete(flush: true)
        flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'userGroupAssociation.label', default: 'UserGroupAssociation'), params.id])}"
        redirect(action: "list")
      } catch (org.springframework.dao.DataIntegrityViolationException e) {
        flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'userGroupAssociation.label', default: 'UserGroupAssociation'), params.id])}"
        redirect(action: "show", id: params.id)
      }
    } else {
      flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'userGroupAssociation.label', default: 'UserGroupAssociation'), params.id])}"
      redirect(action: "list")
    }
  }
}
